// package de.spieleck.servlets;
// ProxyServlet - serving pages from foreign servers....
// From: http://frank.spieleck.de/download/ProxyServlet.java

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *  Store file contained in POST request
 *  @author <a href="mailto:stephen.francis -at- lucidium.net">Stephen Francis</a>.
 */

public class StoreServlet extends HttpServlet {
	public final static String CRLF = "\r\n";		// "Official" HTTP line end
	public final static String LF   = "\n";
	protected ServletContext oServletContext = null;
	protected boolean debugFlag;

	public void init( ServletConfig config ) throws ServletException {
		super.init(config);
		debugFlag = "true".equals( getInitParameter( "debug" ) );
    }

/// Returns a string containing information about the author, version, and
// copyright of the servlet.
	public String getServletInfo() {
		return "Store file contained in POST request";
	}

/// Services a single request from the client.
// @param req the servlet request
// @param req the servlet response
// @exception ServletException when an exception has occurred
	public void service( HttpServletRequest req, HttpServletResponse res )
				throws ServletException, java.io.IOException {
		if ( oServletContext == null )
			oServletContext = req.getSession( true ).getServletContext();
		outputHeader( req );
		try {
			String strPath = req.getSession().getServletContext().getRealPath( "index.html" );
			int iX = strPath.lastIndexOf( "/" );
			if ( iX > -1 )
				strPath = strPath.substring( 0, iX + 1 );
			else
				strPath = strPath.substring( 0, strPath.lastIndexOf( "\\" ) + 1 );		// Windows Hack
			log( "realpath == " + strPath );
			String strTopic = req.getHeader( "topic_id" );
			java.io.FileWriter oFileWriter = new java.io.FileWriter( strPath + "data/" + strTopic + ".xml" );
			// Copy post data
			java.io.BufferedReader oReader = req.getReader();
			String strLine = oReader.readLine();
			while ( strLine != null ) {
				log( strLine );
				oFileWriter.write( strLine + "\n" );
				strLine = oReader.readLine();
			}
			oReader.close();
			oFileWriter.close();
			log( "Request End" );

		} catch ( Exception e ) {
			log( e.toString() );
		}
	}

	public void outputHeader( HttpServletRequest req ) {
		try {
			log( "Request Start" );
			log( "Context Path == " + req.getContextPath() );
			log( "Method == " + req.getMethod() );
			log( "QueryString == " + req.getQueryString() );
			log( "RemoteUser == " + req.getRemoteUser() );
			log( "RequestedSessionId == " + req.getRequestedSessionId() );
			log( "RequestURI == " + req.getRequestURI() );
			log( "RequestURL == " + req.getRequestURL() );
			log( "ServletPath == " + req.getServletPath() );
			java.util.Enumeration en = req.getHeaderNames();
			while ( en.hasMoreElements() ) {
				String strHeader = (String)en.nextElement();
				log( strHeader + " == " + req.getHeader( strHeader ) );
			}
		} catch ( Exception e ) {
			log( e.toString() );
		}
	}

	public void log( String msg ) {
		if ( debugFlag )
			oServletContext.log( msg );
//			System.err.println("## "+msg);
	}

}